package com.speily.study.consumer.listener;


import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Auther: SPL
 * @Date: 2019-07-09 13:50
 * @Description: rocketMq消息监听实现类,消费者
 */
public class MessageListenerImpl implements MessageListenerConcurrently {


    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {

        /**
         * CONSUME_SUCCESS 消费成功
         * RECONSUME_LATER 重试消费，重试次数可以设置，默认是16次
         */

        //处理消息
        for (MessageExt msg : list) {

            //判断是否是事务消息 如果是事务消息则用事务消息的逻辑处理
            try {


                //！！！在此处理业务
                System.out.println(">>>>" + new String(msg.getBody(), "UTF-8"));


            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();

                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
        }
        // 如果没有异常会认为都成功消费
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
}
